<?php

namespace IDock\Insurance\MACN\Response;

use IDock\Http\Response;
use IDock\IDock\IDockEnums;
use IDock\Insurance\MACN\MACNEnums;
use IDock\Printer\Premium\PremiumCoveragePrinter;
use IDock\Printer\Premium\PremiumRelationPrinter;
use IDock\Printer\RenewalInquiryPrinter;
use IDock\Printer\VehiclePrinter;

class RenewalInquiryResponse extends AllMACNResponse
{
    public function printerInit(): Response
    {
        $renewalInquiryPrinter = new RenewalInquiryPrinter();

        $data = $this->getBody() ?? [];
        $renewalInquiryPrinter->renewalFlg = true;
        $applicantPrinter = new PremiumRelationPrinter();
        $insuredPrinter = new PremiumRelationPrinter();
        $ownerPrinter = new PremiumRelationPrinter();
        $vehiclePrinter = new VehiclePrinter();
        $vehicle = $data['itemInfo'] ?? [];
        //车牌号
        $renewalInquiryPrinter->licenseNo = $vehicle['plateNum'] ?? '';
        //车架号
        $renewalInquiryPrinter->vin = $vehicle['frameNo'] ?? '';
        //发动机号
        $renewalInquiryPrinter->engineNo = $vehicle['engineNum'] ?? '';
        //初登日期
        $renewalInquiryPrinter->firstRegisterDate = $vehicle['singeinDate'] ?? '';
        //保单号
        $renewalInquiryPrinter->ciPolicyNo = $data['baseInfo']['efcPolicyNo'] ?? '';
        $renewalInquiryPrinter->biPolicyNo = $data['baseInfo']['bizPolicyNo'] ?? '';
        //投保人
        if ($applicant = $data['holderInfo'] ?? []) {
            $applicantPrinter->name = $applicant['personName'] ?? '';
            $applicantPrinter->mobile = $applicant['telephone'] ?? '';
            $applicantPrinter->address = $applicant['address'] ?? '';
            $applicantPrinter->certificateNo = $applicant['identifyNumber'] ?? '';
            $applicantPrinter->certificateTypeCode = array_search($applicant['identifyType'],MACNEnums::$certificateTypeCode)  ?? '';
            $applicantPrinter->email = $applicant['email'] ?? '';
            $applicantPrinter->certificateStartDate = $applicant['idcardstartdate'] ?? '';
            $applicantPrinter->certificateEndDate = $applicant['idcardenddate'] ?? '';

            $applicantPrinter->typeCode = ($applicant['personType'] == '1' or $applicant['personType'] == '3') ? '01' : '03';

        }

        //被保人
        if ($insured = $data['insuredInfo'] ?? []) {
            $insuredPrinter->name = $insured['personName'] ?? '';
            $insuredPrinter->mobile = $insured['telephone'] ?? '';
            $insuredPrinter->address = $insured['address'] ?? '';
            $insuredPrinter->certificateNo = $insured['identifyNumber'] ?? '';
            $insuredPrinter->certificateTypeCode = array_search($applicant['identifyType'],MACNEnums::$certificateTypeCode)  ?? '';
            $insuredPrinter->email = $insured['email'] ?? '';
            $insuredPrinter->certificateStartDate = $insured['idcardstartdate'] ?? '';
            $insuredPrinter->certificateEndDate = $insured['idcardenddate'] ?? '';

            $insuredPrinter->typeCode = ($insured['personType'] == '1' or $insured['personType']) ? '01' : '03';
        }

        //车主
        $ownerPrinter->name = $vehicle['carOwner'] ?? '';
        $ownerPrinter->mobile = $vehicle['mobilePhone'] ?? '';
        $ownerPrinter->address = $vehicle['address'] ?? '';
        $ownerPrinter->email = $vehicle['email'] ?? '';
        $ownerPrinter->certificateNo = $vehicle['ownerIdentifyNumber'] ?? '';
        $ownerPrinter->certificateTypeCode = array_search($vehicle['identifyType'],MACNEnums::$certificateTypeCode)  ?? '';
        $ownerPrinter->certificateStartDate = $vehicle['idcardstartdate'] ?? '';
        $ownerPrinter->certificateEndDate = $vehicle['idcardenddate'] ?? '';
        $ownerPrinter->typeCode = ($vehicle['insuredType'] == '1' or $vehicle['insuredType']) ? '01' : '03';


        //车辆信息
        $vehiclePrinter->vehicleName = $vehicle['carModelName'] ?? '';
        $vehiclePrinter->brandName = $vehicle['brandName'] ?? '';
        $vehiclePrinter->familyName = '';
        $vehiclePrinter->power = $vehicle['power'] ?? '';
        $vehiclePrinter->vehiclePrice = $vehicle['actualValue'] ?? '';
        $vehiclePrinter->vehiclePriceTax = $vehicle['purchasePrice'] ?? '';
        $vehiclePrinter->vehicleExhaust = $vehicle['exhaustScale'] ?? '';
        $vehiclePrinter->vehicleSeat = $vehicle['seatCount'] ?? '';
        $vehiclePrinter->vehicleTonnage = $vehicle['tonCount'] ?? '';

        //保单信息

        //交强险
        if ($jqData = $data['forceRiskInfo'] ?? []) {
            $renewalInquiryPrinter->ciStartData = $jqData['startDate'] ?? '';
            $renewalInquiryPrinter->ciEndData = $jqData['endDate'] ?? '';
        }
        if ($application = $data['businessRiskInfo'] ?? []) {
            //商业险
            $renewalInquiryPrinter->biStartData = $application['startDate'] ?? '';
            $renewalInquiryPrinter->biEndData = $application['endDate'] ?? '';
            //商业险险种
            if ($coverageList = $application['itemKindList']['itemKindInfo'] ?? []) {
                foreach ($coverageList as $coverage) {
                    $coveragePrinter = new PremiumCoveragePrinter();

                    $coveragePrinter->coverageCode = array_search($coverage['kindCode'], MACNEnums::$coverageCode);

                    if (!$coveragePrinter->coverageCode) {
                        continue;
                    }

                    $coveragePrinter->coverageName = IDockEnums::$coverageCode[$coveragePrinter->coverageCode];
                    $coveragePrinter->amount = $coverage['sumInsured'] ?? '0';
                    $coveragePrinter->unitAmount = $coverage['unitInsured'] ?? '0';
                    $coveragePrinter->quantity = $coverage['quantity'] ?? '0';
                    $coveragePrinter->discount = $coverage['discount'] ?? '0';

                    $renewalInquiryPrinter->setCoveragePrinterList($coveragePrinter);
                }
            }
        }

        $renewalInquiryPrinter->setApplicantPrinter($applicantPrinter);
        $renewalInquiryPrinter->setInsuredPrinter($insuredPrinter);
        $renewalInquiryPrinter->setOwnerPrinter($ownerPrinter);
        $renewalInquiryPrinter->setVehiclePrinter($vehiclePrinter);

        $this->printer = $renewalInquiryPrinter;

        return $this;
    }
}
